package com.ruoyi.medical.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.medical.domain.ConsultOrder;
import com.ruoyi.medical.service.IConsultOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 咨询管理Controller
 * 
 * @author WGH
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/medical/consultOrder")
public class ConsultOrderController extends BaseController
{
    @Autowired
    private IConsultOrderService consultOrderService;



    /**
     * 创建订单
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody ConsultOrder consultOrder) {
        consultOrder.setCreateTime(new Date());
        consultOrder.setDel(0);
        int result = consultOrderService.insertConsultOrder(consultOrder);

        if (result > 0) {
            // 直接返回订单ID作为 orderNo
            return success(consultOrder.getId());
        } else {
            return error("创建订单失败");
        }
    }

    /**
     * 支付订单
     */
    @PostMapping("/pay/{id}")
    public AjaxResult pay(@PathVariable Long id, @RequestBody Map<String, String> param) {
        String payMethod = param.get("payMethod");
        ConsultOrder order = consultOrderService.selectConsultOrderById(id);
        if (order == null) {
            return error("订单不存在");
        }

        // 暂时用 del 代替：0正常，1已支付
        order.setStatus(1);

        consultOrderService.updateConsultOrder(order);

        return success("支付成功（支付方式：" + payMethod + "）");
    }

    /**
     * 查询咨询管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:consultOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConsultOrder consultOrder)
    {
        startPage();
        List<ConsultOrder> list = consultOrderService.selectConsultOrderList(consultOrder);
        return getDataTable(list);
    }

    /**
     * 导出咨询管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:consultOrder:export')")
    @Log(title = "咨询管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConsultOrder consultOrder)
    {
        List<ConsultOrder> list = consultOrderService.selectConsultOrderList(consultOrder);
        ExcelUtil<ConsultOrder> util = new ExcelUtil<ConsultOrder>(ConsultOrder.class);
        util.exportExcel(response, list, "咨询管理数据");
    }

    /**
     * 获取咨询管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('medical:consultOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(consultOrderService.selectConsultOrderById(id));
    }

    /**
     * 新增咨询管理
     */
    @PreAuthorize("@ss.hasPermi('medical:consultOrder:add')")
    @Log(title = "咨询管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConsultOrder consultOrder)
    {
        return toAjax(consultOrderService.insertConsultOrder(consultOrder));
    }

    /**
     * 修改咨询管理
     */
    @PreAuthorize("@ss.hasPermi('medical:consultOrder:edit')")
    @Log(title = "咨询管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConsultOrder consultOrder)
    {
        return toAjax(consultOrderService.updateConsultOrder(consultOrder));
    }

    /**
     * 删除咨询管理
     */
    @PreAuthorize("@ss.hasPermi('medical:consultOrder:remove')")
    @Log(title = "咨询管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(consultOrderService.deleteConsultOrderByIds(ids));
    }
}
